home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / gnuplot / contrib / clift / pipelib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-08  |  1.3 KB  |  102 lines

  1. /*
  2.   pipeLib.c
  3.  
  4.   by Simon Clift
  5.   University of Waterloo, CS Dept., SciCom Group
  6.  
  7.   Use this at your own risk.
  8.  
  9.   A small library of functions to allow FORTRAN 77 on the Sun to use
  10.   pipes to talk to other programs interactively.
  11.  
  12. */
  13.  
  14. #include <stdio.h>
  15.  
  16.  
  17. /*
  18.   Allow an array of file descriptors
  19. */
  20.  
  21. #define    MaxPipes        20
  22. #define    PipeOpen        -1
  23. #define    PipeClosed        0
  24.  
  25. FILE        *pipes[MaxPipes];
  26. int        pipes_open[MaxPipes];        /* status */
  27.  
  28. /*
  29.   plinit_
  30.  
  31.   Initialize pipes system
  32. */
  33.  
  34. void plinit_( void )
  35. {
  36.     int     i;
  37.  
  38.     for( i=0 ; i < MaxPipes ; i++ ) {
  39.         pipes_open[i] = PipeClosed;
  40.     }
  41. }
  42.  
  43. /*
  44.   plopen_
  45.  
  46.   Open a pipe to a program given the startup command, and either "r" or "w"
  47.  
  48.   Status variable   stat = Pipe number on successful open, -1 otherwise
  49. */
  50.  
  51. void plopen_( char * command, char * mode, int * stat )
  52. {
  53.     int    i;
  54.  
  55.     for( i=0 ; i < MaxPipes ; i++ ) {
  56.         if( pipes_open[i] == PipeClosed ) {
  57.             break;
  58.         }
  59.     }
  60.  
  61.     if( i == MaxPipes ) {
  62.         *stat = -1;
  63.     } else {
  64.         pipes[i] = popen( command, mode );
  65.         *stat = (i+1);
  66.     }
  67. }
  68.  
  69. /* 
  70.   plwrit_
  71.  
  72.   Write a string to a pipe.
  73.  
  74. */
  75.  
  76. void plwrit_( int * pipeNum, char * stringOut )
  77. {
  78.     fprintf( pipes[*pipeNum-1], "%s\n", stringOut );
  79. }
  80.  
  81. /*
  82.   plflus_
  83.  
  84.   Flush a pipe's output
  85. */
  86.  
  87. void plflus_( int *pipeNum )
  88. {
  89.     fflush( pipes[ *pipeNum - 1 ] );
  90. }
  91.  
  92. /*
  93.   plclos_
  94.  
  95.   Close a pipe
  96. */
  97.  
  98. void plclos_( int * pipeNum )
  99. {
  100.     pclose( pipes[*pipeNum-1] );
  101. }
  102.